function main() {
    // 督办下发
    const self = this;
    debugger

    let treeSelect = self.$$("tree_panel_9u8epp").$$getSelection().selectedRows;
    if (treeSelect.length === 0) {
        self.$message.warn("请选择下发督办对象");
        return;
    }

    const taskId = self.$$getRouteView().$$getRenderElement().extraParams.taskId;
    const objectId = self.$$getRouteView().$$getRenderElement().extraParams.objectId.replace('unit', '');

    if (!taskId) {
        self.$message.warn("请重新选择督办下发！");
        return;
    }

    let taskData = {
        taskId: taskId,
        objectId: treeSelect[0].id.replace('unit', ''),
        objectName: treeSelect[0].name
    }

    let username = api.USER_INFO().name;

    let handleRecord = {
        replyId: taskId,
        recordType: 2,
        recordTitle: "督办下发",
        recordContent: `督办人员${username}下发督办事项至${treeSelect[0].name}办理。`,
        relationReplyId: ""
    }

    insertTaskObject(taskData).then(res => {
        // 修改law_std_sup_clue.status 2=已移交
        insertHandleRecord(handleRecord).then(r => {
            updateObjectStatus(objectId);
            notifyParentAndClose(self)
        })

    })

}

/**
 * 修改线索状态
 */
function updateObjectStatus(objectId) {

    const law_std_sup_task_object = "ds_tbl:t_c232a327f2a44a8dbfa0c5a1c74a2026-9e5a";
    api.EXEC_FORMULA("UPDATE('" + law_std_sup_task_object + "', QUERY_EQ('id', '" + objectId + "'), '', ['receive_status', '" + 3 + "','finish_status','1'])").then(() => {

        console.log("res", "执行结果,修改成功！");
        let pageListEle = window.taskPageList;
        // 获取列表渲染页实例
        const listRenderView = pageListEle.$$getRenderElement();
        // 获取当前显示的组件，表格或者卡片
        const currentListEle = listRenderView.$$getListElement();
        // 调用刷新方法
        currentListEle.$$refreshData();

    }).catch(err => {
        self.$message.error(err.message); // 显示错误提示信息
    });
}

/**
 * 处理经过
 * @param recordData
 * @returns {Promise<unknown>}
 */
function insertHandleRecord(recordData) {
    return new Promise((resolve, reject) => {
        api.EXEC_FORMULA("API_CALL('ds_api:deef77e2-7e67-4bae-acbd-cbd468cfca82-9e5a', 'userId',USER_ID(), 'result.uid')").then(res => {
            let userId = res.result;
            const insertHandleRecords = [
                ['record_uid', 'UUID()'],
                ['relation_id', `'${recordData.replyId}'`],
                ['record_type', `'${recordData.recordType}'`],
                ['record_title', `'${recordData.recordTitle}'`],
                ['record_content', `'${recordData.recordContent}'`],
                ['create_time', 'NOW()'],
                ['create_human_id', `'${userId}'`],
                ['relation_reply_id', `'${recordData.relationReplyId}'`]
            ];

            const formattedFields = insertHandleRecords.map(([key, value]) => {
                return `'${key}',${value}`;
            });

            const law_std_sup_handle_record = "ds_tbl:t_0ff47ea0e3a74ffdbc274409e7103e7e-9e5a";
            const insertSql = `INSERT('${law_std_sup_handle_record}', [${formattedFields.join(',')}])`;
            console.log(insertSql);


            api.EXEC_FORMULA(insertSql).then(res => {
                console.log(res);
                resolve(res);
            })
        })
    });


}


/**
 * 新增下发督办
 * @param taskData
 */
function insertTaskObject(clueData) {
    return new Promise((resolve, reject) => {
        api.EXEC_FORMULA("API_CALL('ds_api:deef77e2-7e67-4bae-acbd-cbd468cfca82-9e5a', 'userId',USER_ID(), 'result.uid')").then(res => {

            let userId = res.result;
            api.DATA_SOURCE_EXECUTE("ds_mod:bcc76ab7-aa47-483a-bb7a-bd074476e003", "ddcat", [], [], "").then(res => {
                let unit_id = res.result.uid
                const insertTaskObjects = [
                    ['task_id', clueData.taskId],
                    ['opt_human_id', `'${userId}'`],
                    ['lead_unit_id', `'${unit_id}'`],
                    ['object_id', `'${clueData.objectId}'`],
                    ['object_name', `'${clueData.objectName}'`],
                    ['object_type', '1'],
                    ['receive_status', '0'],
                    ['finish_status', '0'],
                    ['delete_flag', `'0'`],
                    ['create_time', 'NOW()'],
                    ['create_human_id', `'${userId}'`],
                ];

                const formattedFields = insertTaskObjects.map(([key, value]) => {
                    return `'${key}',${value}`;
                });

                const law_std_sup_task_object = "ds_tbl:t_c232a327f2a44a8dbfa0c5a1c74a2026-9e5a";
                const insertSql = `INSERT('${law_std_sup_task_object}', [${formattedFields.join(',')}])`;
                console.log(insertSql);


                api.EXEC_FORMULA(insertSql).then(res => {
                    console.log(res);
                    resolve(res);
                })
            })
        })
    });
}

function notifyParentAndClose(self) {
    const pageRenderElement = self.$$root().$parent.$parent.$parent;
    if (window.parent) {
        window.parent.postMessage(
            JSON.stringify({ event: "close-dialog", param: { XXX: "test" } }),
            "*"
        );
    }
}
